package org.me.marble;

import java.util.HashMap;

import android.media.SoundPool;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool.OnLoadCompleteListener;


/**
*
* @author tungi
*/

public class SoundManager implements OnLoadCompleteListener{ 
	private static final int MAX_NUMBER_OF_MUSIC=11;
	
	private static Context mContext;
	private static SoundPool mSoundPool;
	private static SoundManager _instance;
	private static AudioManager  mAudioManager;
	private static boolean loadCompleted=false;
	private static HashMap<Integer,Integer> mSoundPoolMap;
	private static HashMap<Integer,Integer> playingSounds;
	
	private int loadedSounds=0;
 
	private SoundManager(){
	}
	
	//using singleton design pattern
	static synchronized public SoundManager getInstance(){
	    if (_instance==null)
	      _instance=new SoundManager();
	    return _instance;
	 }
	
	//initialize part
	public static  void initSounds(Context theContext){
		 mContext=theContext;
	     mSoundPool=new SoundPool(4,AudioManager.STREAM_MUSIC,0);
	     mSoundPoolMap=new HashMap<Integer,Integer>();
	     mAudioManager=(AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
	     playingSounds=new HashMap<Integer,Integer>();
	} 
 
	//add sound to pool
	public static void addSound(int Index,int SoundID){
		mSoundPoolMap.put(Index,mSoundPool.load(mContext,SoundID,1));
	}

	//load sounds
	public static void loadSounds(){
		mSoundPoolMap.put(sounds.Win.ordinal(),mSoundPool.load(mContext,R.raw.win,1));
		mSoundPoolMap.put(sounds.Fail.ordinal(),mSoundPool.load(mContext,R.raw.fail,1));
		mSoundPoolMap.put(sounds.PointObject.ordinal(),mSoundPool.load(mContext,R.raw.point,1));
		mSoundPoolMap.put(sounds.LifeObject.ordinal(),mSoundPool.load(mContext,R.raw.life,1));
		mSoundPoolMap.put(sounds.FastMarkObject.ordinal(),mSoundPool.load(mContext,R.raw.fast,1));
		mSoundPoolMap.put(sounds.Gate.ordinal(),mSoundPool.load(mContext,R.raw.gate,1));
		mSoundPoolMap.put(sounds.SlowMarkObject.ordinal(),mSoundPool.load(mContext,R.raw.slow,1));
		mSoundPoolMap.put(sounds.QuestionMarkObject.ordinal(),mSoundPool.load(mContext,R.raw.question,1));
		mSoundPoolMap.put(sounds.InvertMarkObject.ordinal(),mSoundPool.load(mContext,R.raw.invert,1));
		//mSoundPoolMap.put(sounds.Main.ordinal(),mSoundPool.load(mContext,R.raw.menu,1));
		//mSoundPoolMap.put(sounds.Loading.ordinal(),mSoundPool.load(mContext,R.raw.loading,1));
		mSoundPoolMap.put(sounds.Cuboid.ordinal(),mSoundPool.load(mContext,R.raw.block,1));
		mSoundPoolMap.put(sounds.PointObject2.ordinal(),mSoundPool.load(mContext,R.raw.point2,1));
		//mSoundPoolMap.put(sounds.Game.ordinal(),mSoundPool.load(mContext,R.raw.game,1));
		
		mSoundPool.setOnLoadCompleteListener(_instance);
	}
 
	//////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////
	/**
	 * @param index The index of the sound to be played
	 * @param loop 0 - no loop, -1 - loop forever
	 * @param speed The speed, 1.0 for normal, valid range is 0.5 - 2.0
	 */
	public static void playSound(int index,int loop,float speed){
		try{
		     float streamVolume=mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		     streamVolume=streamVolume/mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		     int soundID=mSoundPool.play(mSoundPoolMap.get(index),streamVolume,streamVolume,1,loop,speed);
		     if(soundID!=0)//in this case streaming was successful
		    	 playingSounds.put(index,soundID);
		}catch(NullPointerException npx){}
	}

	public static void stopSound(int index){
		try{
			mSoundPool.setLoop(playingSounds.get(index),0);
			mSoundPool.stop(playingSounds.get(index));
		}catch(NullPointerException npx){}
	}

	//////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////
	public static void cleanup(){
		mSoundPool.release();
		mSoundPool=null;
	    mSoundPoolMap.clear();
	    mAudioManager.unloadSoundEffects();
	    _instance=null;
 	}
	
	//////////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////////
	@Override
	public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
		loadedSounds++;
		if(loadedSounds==MAX_NUMBER_OF_MUSIC)
			loadCompleted=true;
	}
	
	public static boolean isLoadCompleted(){
		return loadCompleted;
	}
}